﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_EaspLibs;
using System.Data;

namespace V5_FrameWork.FrameWork {
    /// <summary>
    /// 
    /// </summary>
    public class BaseClassHelper {
        /// <summary>
        /// 根据表名获取分类下拉列表
        /// </summary>
        /// <param name="strFrom">表名称</param>
        /// <param name="ChannelID">频道ID</param>
        /// <param name="strMsg">第一列显示的信息</param>
        /// <returns></returns>
        public DataTable GetDropDownList(string strFrom, int ChannelID, string strMsg) {
            DataTable listClass = Easp.DB.Query_By("*", strFrom, "", "ClassCode", 0).Tables[0];
            DataTable dropdt = new DataTable();
            dropdt.Columns.Add("ClassID");
            dropdt.Columns.Add("ClassName");
            if (strMsg.Trim() != "") {
                DataRow dr = dropdt.NewRow();
                dr["ClassID"] = "0";
                dr["ClassName"] = strMsg;
                dropdt.Rows.Add(dr);
            }
            if (listClass != null) {
                foreach (DataRow model in listClass.Rows) {
                    DataRow dr = dropdt.NewRow();
                    dr["ClassID"] = model["ClassID"].ToString();
                    dr["ClassName"] = GetColumnListName(model["ClassName"].ToString(), model["ClassCode"].ToString());
                    dropdt.Rows.Add(dr);
                }
            }
            return dropdt;
        }
        /// <summary>
        /// 分类显示
        /// </summary>
        /// <param name="sName">分类名称</param>
        /// <param name="sCode">分类ClassCode</param>
        /// <returns></returns>
        public string GetColumnListName(string sName, string sCode) {
            int Level = (sCode.Length / 4 - 1) * 2;
            string sStr = sCode.Length == 4 ? "" : "├";
            if (Level > 0) {
                for (int i = 0; i < Level; i++)
                    sStr += "－";
            }
            return sStr + sName;
        }
        /// <summary>
        /// 移动分类
        /// </summary>
        /// <param name="strFrom"></param>
        /// <param name="ClassID"></param>
        /// <param name="isUp"></param>
        public void MoveClass(string strFrom, int ClassID, int isUp) {
            string oldCode = string.Empty;
            oldCode = Convert.ToString("" + Easp.DB.ExecuteScalar("Select ClassCode From " + strFrom + " Where ClassID=" + ClassID));
            string subStr = "substring";
            int codeLen = oldCode.Length;
            if (codeLen > 1) {
                string temp = string.Empty;
                string wStr = "";
                string newStr = "";
                for (int i = 0; i < codeLen; i++)
                    newStr += "-";
                if (codeLen > 4)
                    wStr = " and left(ClassCode," + Convert.ToString(codeLen - 4) + ")='" + oldCode.Substring(0, codeLen - 4) + "'";
                if (isUp == 1)
                    wStr = "select top 1 ClassCode from " + strFrom + " where len(ClassCode)=" + codeLen.ToString() + " and ClassCode<'" + oldCode + "'" + wStr + " order by ClassCode desc ";
                else
                    wStr = "select top 1 ClassCode from " + strFrom + " where len(ClassCode)=" + codeLen.ToString() + " and ClassCode>'" + oldCode + "'" + wStr + " order by ClassCode ";
                temp = Convert.ToString("" + Easp.DB.ExecuteScalar(wStr));
                if (temp.Length > 1) {
                    //Move Under Column
                    wStr = "update " + strFrom + " set ClassCode='" + newStr + "'+" + subStr + "(ClassCode," + Convert.ToString(codeLen + 1) + ",len(Classcode)) where left(ClassCode," + codeLen.ToString() + ")='" + temp + "'";
                    Easp.DB.ExecuteNonQuery(wStr);
                    //Update Target Column
                    wStr = "update " + strFrom + " set classcode='" + temp + "'+" + subStr + "(classcode," + Convert.ToString(codeLen + 1) + ",len(Classcode)) where left(classcode," + codeLen.ToString() + ")='" + oldCode + "'";
                    Easp.DB.ExecuteNonQuery(wStr);
                    //Update Under Column
                    wStr = "update " + strFrom + " set classcode='" + oldCode + "'+" + subStr + "(classcode," + Convert.ToString(newStr.Length + 1) + ",len(ExecuteNonQuery)) where left(Classcode," + newStr.Length.ToString() + ")='" + newStr + "'";
                    Easp.DB.ExecuteNonQuery(wStr);
                    //以下是更新栏目信息 可以不用添加
                }
            }
        }

        /// <summary>
        /// 分类添加更新
        /// </summary>
        /// <param name="strFrom"></param>
        /// <param name="Result"></param>
        /// <param name="ClassID"></param>
        /// <param name="ParentID"></param>
        public void ClassAddUpdate(string strFrom, int Result, int ClassID, int ParentID) {
            #region 开始更新 ClassCode
            string SQLString = string.Empty;
            string leftCode = string.Empty, selfCode = string.Empty, parentCode = string.Empty;
            if (Result > 0) {
                if (ParentID != 0) {
                    parentCode = Convert.ToString("" + Easp.DB.ExecuteScalar("Select ClassCode as NewName From " + strFrom + " Where ClassID=" + ParentID));
                }
                SQLString = @"
                select Classcode as NewName from " + strFrom + @"
                where left(Classcode," + parentCode.Length.ToString() + @")='" + parentCode + @"' and Len(ClassCode)=" + Convert.ToString(parentCode.Length + 4) + @"
                order by Classcode desc
            ";
                leftCode = Convert.ToString("" + Easp.DB.ExecuteScalar(SQLString));
                if (leftCode.Length > 0) {
                    selfCode = Convert.ToString(Convert.ToInt32(leftCode.Substring(leftCode.Length - 4, 4)) + 1).PadLeft(4, '0');
                }
                else {
                    selfCode = "0001";
                }
                selfCode = parentCode + selfCode;
                Result = Easp.DB.ExecuteNonQuery("Update " + strFrom + " Set ClassCode='" + selfCode + "' Where ClassID=" + ClassID);
            }
            #endregion
        }
        /// <summary>
        /// 分类修改更新
        /// </summary>
        /// <param name="strFrom"></param>
        /// <param name="Result"></param>
        /// <param name="ClassID"></param>
        /// <param name="ParentID"></param>
        public void ClassUpdateUpdate(string strFrom, int Result, int ClassID, int ParentID) {
            #region 开始更新
            string SQLString = string.Empty;
            string leftCode = string.Empty, selfCode = string.Empty, parentCode = string.Empty, oldCode = string.Empty, sourceCode = string.Empty;
            SQLString = "Select ClassCode From " + strFrom + " Where ClassID=" + ParentID;
            parentCode = Convert.ToString("" + Easp.DB.ExecuteScalar(SQLString));
            SQLString = "Select ClassCode From " + strFrom + " Where ClassID=" + ClassID;
            oldCode = Convert.ToString("" + Easp.DB.ExecuteScalar(SQLString));
            sourceCode = oldCode;
            oldCode = oldCode.Substring(0, oldCode.Length - 4);
            if (oldCode != parentCode) {
                SQLString = @"
                select Classcode as NewName from " + strFrom + @"
                where left(Classcode," + parentCode.Length.ToString() + @")='" + parentCode + @"' and Len(ClassCode)=" + Convert.ToString(parentCode.Length + 4) + @"
                order by Classcode desc ";
                leftCode = Convert.ToString("" + Easp.DB.ExecuteScalar(SQLString));
                if (leftCode.Length > 0) {
                    selfCode = Convert.ToString(Convert.ToInt32(leftCode.Substring(leftCode.Length - 4, 4)) + 1).PadLeft(4, '0');
                }
                else {
                    selfCode = "0001";
                }
                selfCode = parentCode + selfCode;
                Result = Easp.DB.ExecuteNonQuery("Update " + strFrom + " Set ClassCode='" + selfCode + "' Where ClassID=" + ClassID);
                Result = Easp.DB.ExecuteNonQuery("update " + strFrom + " Set ClassCode='" + selfCode + "'+right(ClassCode,len(ClassCode)-" + sourceCode.Length.ToString() + ") where ClassCode like '" + sourceCode + "%'");
            }
            #endregion
        }
    }
}
